package ui;

import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import commons.WorkSpace;

public class SpaceFrame extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel mainPanel;
	private JLabel infoLabel;
	private JPanel choosePanel;
	private JLabel workTextLabel;
	
	private JTextField pathText;//用户选择的文件目录
	private JButton chooseButton;
	private JPanel buttonPanel;
	
	private JButton confirmButton;
	private JButton cancelButton;
	private SpaceChooser chooser;
	
	private File folder;
	
	/**
	 * Create the frame.
	 */
	public SpaceFrame(EditorFrame editorFrame) {
		mainPanel = new JPanel();
		infoLabel = new JLabel("请选择工作空间");
		choosePanel = new JPanel();
		workTextLabel = new JLabel("工作空间");
		pathText = new JTextField("",40);
		chooseButton = new JButton("选择");
		buttonPanel = new JPanel();
		confirmButton = new JButton("确定");
		cancelButton = new JButton("取消");
		chooser = new SpaceChooser(this);
		
		//设置mainPanel布局
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainPanel.add(infoLabel);
		
		//设置choosepanel
		choosePanel.setLayout(new BoxLayout(choosePanel, BoxLayout.X_AXIS));
		chooseButton.addActionListener(new ChooseButtonListener(chooser));
		
		pathText.setEditable(false);
		choosePanel.add(workTextLabel);
		choosePanel.add(pathText);
		choosePanel.add(chooseButton);
		
		mainPanel.add(choosePanel);
		
		confirmButton.setEnabled(false);
		//确定按钮添加事件
		confirmButton.addActionListener(new ConfirmButtonListener(this,editorFrame));
		buttonPanel.add(confirmButton);
		buttonPanel.add(new Label("   "));
		buttonPanel.add(cancelButton);
		//取消按钮添加退出事件
		cancelButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		
		mainPanel.add(buttonPanel);
		add(mainPanel);
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(300,200);
		setResizable(false);
		
	}
	
	public File getFolder(){
		return folder;
	}
	
	public void setFolder(File folder){
		this.folder = folder;
	}
	
	public JTextField getPathText(){
		return pathText;
	}
	
	public JButton getConfirmButton(){
		return confirmButton;
	}
	
	//确定按钮的监听器
	class ConfirmButtonListener implements ActionListener{
		private SpaceFrame spaceFrame;
		private EditorFrame editorFrame;
		
		public ConfirmButtonListener(SpaceFrame spaceFrame, EditorFrame editorFrame){
			this.spaceFrame = spaceFrame;
			this.editorFrame = editorFrame;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//初始化editorFrame的界面
			editorFrame.initFrame(new WorkSpace(spaceFrame.getFolder(),editorFrame));
			//设为可见
			editorFrame.setVisible(true);
			editorFrame.setSize(900,600);
			//spaceFrame不可见
			spaceFrame.setVisible(false);
		}
	}
	
	//选择按钮的监听器
	class ChooseButtonListener implements ActionListener{
		private JFileChooser chooser;
		
		public ChooseButtonListener(JFileChooser chooser){
			this.chooser = chooser;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//只选择目录
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			chooser.showOpenDialog(null);
		}
	}
	
	//文件选择器
	class SpaceChooser extends JFileChooser{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private SpaceFrame spaceFrame;
		
		//将SpaceFrame作为参数
		public SpaceChooser(SpaceFrame spaceFrame){
			//设置选择器打开时的目录
			super("/");
		
			this.spaceFrame = spaceFrame;
		}
		
		//重写选择文件的方法
		public void approveSelection(){
			//获取用户选择的文件
			File folder = getSelectedFile();
			spaceFrame.setFolder(folder);
			//设置spaceframe文本框
			spaceFrame.getPathText().setText(folder.getAbsolutePath());
			spaceFrame.getConfirmButton().setEnabled(true);
			super.approveSelection();
			
		}
	}

}
